package net.realsee.service.wcs.task.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.task.WcsTaskStep;
import net.realsee.domain.wcs.task.bo.WcsTaskStepBo;
import net.realsee.domain.wcs.task.vo.WcsTaskStepVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.task.WcsTaskStepMapper;
import net.realsee.service.wcs.task.IWcsTaskStepService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 任务模版步骤Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsTaskStepServiceImpl extends ServiceImpl<WcsTaskStepMapper, WcsTaskStep> implements IWcsTaskStepService {

    private final WcsTaskStepMapper baseMapper;

    /**
     * 查询任务模版步骤
     */
    @Override
    public WcsTaskStepVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询任务模版步骤列表
     */
    @Override
    public TableDataInfo<WcsTaskStepVo> selectVoPage(WcsTaskStepBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsTaskStep> lqw = buildVoQueryWrapper(bo);
        Page<WcsTaskStepVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询任务模版步骤列表
     */
    @Override
    public List<WcsTaskStepVo> selectVoList(WcsTaskStepBo bo) {
        LambdaQueryWrapper<WcsTaskStep> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsTaskStep> buildVoQueryWrapper(WcsTaskStepBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsTaskStep> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsTaskStep::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskTempCode()), WcsTaskStep::getTaskTempCode, bo.getTaskTempCode());
        lqw.eq(StringUtils.isNotBlank(bo.getStepCode()), WcsTaskStep::getStepCode, bo.getStepCode());
        lqw.eq(StringUtils.isNotBlank(bo.getStepCompCode()), WcsTaskStep::getStepCompCode, bo.getStepCompCode());
        lqw.like(StringUtils.isNotBlank(bo.getStepName()), WcsTaskStep::getStepName, bo.getStepName());
        lqw.eq(StringUtils.isNotBlank(bo.getStepDesc()), WcsTaskStep::getStepDesc, bo.getStepDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getStepGroupCode()), WcsTaskStep::getStepGroupCode, bo.getStepGroupCode());
        lqw.eq(bo.getTaskStepSeqno() != null, WcsTaskStep::getTaskStepSeqno, bo.getTaskStepSeqno());
        lqw.eq(bo.getAsyncFlag() != null, WcsTaskStep::getAsyncFlag, bo.getAsyncFlag());
        lqw.eq(bo.getBootType() != null, WcsTaskStep::getBootType, bo.getBootType());
        lqw.eq(StringUtils.isNotBlank(bo.getBootTime()), WcsTaskStep::getBootTime, bo.getBootTime());
        lqw.eq(StringUtils.isNotBlank(bo.getJobPoolCode()), WcsTaskStep::getJobPoolCode, bo.getJobPoolCode());
        lqw.eq(StringUtils.isNotBlank(bo.getLoopType()), WcsTaskStep::getLoopType, bo.getLoopType());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsTaskStep::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsTaskStep::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsTaskStep::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsTaskStep::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsTaskStep::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsTaskStep::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsTaskStep::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsTaskStep::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsTaskStep::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsTaskStep::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsTaskStep::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsTaskStep::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsTaskStep::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增任务模版步骤
     */
    @Override
    public Boolean insertByBo(WcsTaskStepBo bo) {
        WcsTaskStep add = BeanUtil.toBean(bo, WcsTaskStep.class);
        return this.save(add);
    }

    /**
     * 修改任务模版步骤
     */
    @Override
    public Boolean updateByBo(WcsTaskStepBo bo) {
        WcsTaskStep update = BeanUtil.toBean(bo, WcsTaskStep.class);
        return this.updateById(update);
    }

    /**
     * 批量删除任务模版步骤
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
